'use strict';
const Controller = require('egg').Controller;

class TodoController extends Controller {
    async add() {
        const { ctx, app } = this;

        const userid = ctx.request.query.userid;
        const type = ctx.request.query.type;
        const content = ctx.request.query.content;
        let count = 1;

        const res = await app.mysql.insert('todo', { userid: userid, type: type, content: content });
        if(res.affectedRows === 1) {
            console.log("200");
            ctx.body = {
                code: 200,
                message: '添加成功',
                data: count++
            }
        } else {
            ctx.body = {
                code: 500,
                message: '添加失败',
                data: null
            }
        }
    }

    async delete() {
        const { ctx, app } = this;

        const id = ctx.query.todoId;
        const res = await app.mysql.delete('todo', {
            id: id
        })
        if(res) {
            ctx.body = {
                code: 200,
                message: '删除成功',
                data: null
            }
        } else {
            ctx.body = {
                code: 500,
                message: '删除失败',
                data: null
            }
        }
    }

    async update() {
        const { ctx, app } = this;

        const id = ctx.request.query.todoId;
        const content = ctx.request.query.content;
        const row = {
            id: id,
            content: content
        }

        const res = await app.mysql.update('todo', row);
        if(res.affectedRows === 1) {
            ctx.body = {
                code: 200,
                message: '修改成功',
                data: null
            }
        } else {
            ctx.body = {
                code: 500,
                message: '修改失败',
                data: null
            }
        }
    }

    async check() {
      const { ctx, app } = this;
      const id = ctx.request.query.todoId;
      const row = {
          id: id,
          checked: 1
      }
      const res = await app.mysql.update('todo', row);
      if(res.affectedRows === 1) {
          ctx.body = {
              code: 200,
              message: '修改成功',
              data: null
          }
      } else {
          ctx.body = {
              code: 500,
              message: '修改失败',
              data: null
          }
      }
    }

    async read() {
        const { ctx, app } = this;

        const userid = ctx.request.query.userid;
        const type = ctx.request.query.type;

        const res = await app.mysql.select('todo', {
            where: { userid: userid, type: type },
            columns: ['id', 'content','checked'],
        });

        if(res) {
            console.log(res);
            ctx.body = {
                code: 200,
                message: '查询成功',
                data: res
            }
        } else {
            ctx.body = {
                code: 200,
                message: '查询失败',
                data: []
            }
        }
    }

    async default() {
        const { ctx, app } = this;

        const userid = ctx.request.query.userid;
        const default_data = [
            ['看电视剧', 1],
            ['看电影', 1],
            ['做美食', 1],
            ['和好朋友出去玩', 1],
            ['短途旅行', 1],
            ['练字', 2],
            ['练习绘画', 2],
            ['练习演讲', 2],
            ['练歌', 2],
            ['研究穿搭', 2],
            ['15分钟整理书桌', 3],
            ['30分钟高强度健身', 3],
            ['15分钟自我梳理', 3],
            ['45分钟完成重要的工作', 3],
            ['正念冥想', 3],
            ['每天固定写作', 4],
            ['定期整理物品', 4],
            ['定期阅读', 4],
            ['定期社交', 4],
            ['写感恩日记，记录3件值得感恩的事情', 4],
        ]

        default_data.forEach((item) => {
            item.unshift(userid);
        })

        console.log(default_data);
        const res = await this.app.mysql.query('insert into todo (userid, content, type) values ?', [default_data]);
        console.log(res);
        if(res.affectedRows === 20) {
            console.log("200");
            ctx.body = {
                code: 200,
                message: '添加成功',
                data: null
            }
        } else {
            ctx.body = {
                code: 500,
                message: '添加失败',
                data: null
            }
        }
    }
}

module.exports = TodoController;